package com.example.yjhuser.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yjhuser.entity.Tiezi;
import com.example.yjhuser.mapper.TieziMapper;
import com.example.yjhuser.mapper.UserMapper;
import com.example.yjhuser.service.TieziService;
import com.yc.yjh.entity.Users;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TieziServiceImpl implements TieziService {

    @Resource
    TieziMapper tieziMapper;

    @Resource
    UserMapper tzUserMapper;
    @Override
    public List<Tiezi> getAllTiezi() {
        QueryWrapper<Tiezi> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        final List<Tiezi> tiezis = tieziMapper.selectList(wrapper);
        tiezis.forEach(tiezi -> {
            final Users users = tzUserMapper.selectById(tiezi.getUid());
            tiezi.setUser(users);
        });
        return tiezis;
    }

    @Override
    public int setTiezi(Tiezi tiezi) {

        return tieziMapper.insert(tiezi);
    }

    @Override
    public Tiezi getTieziById(String id) {
         Tiezi tiezi = tieziMapper.selectById(id);
        final Users users = tzUserMapper.selectById(tiezi.getUid());
        tiezi.setUser(users);
        return tiezi;
    }

    @Override
    public int delTiezById(String id) {
       return  tieziMapper.deleteById(id);
    }

    @Override
    public List<Tiezi> getAllTzByUid(String id) {
        QueryWrapper<Tiezi> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",id).orderByDesc("create_time");
        final List<Tiezi> tiezis = tieziMapper.selectList(wrapper);
        tiezis.forEach(tiezi -> {
            final Users users = tzUserMapper.selectById(tiezi.getUid());
            tiezi.setUser(users);
        });
        return tiezis;
    }
}
